#!/bin/sh
#encoding=utf-8
'''
Created on 2016-1-7
@author: zhaojun
'''
from apscheduler.schedulers.blocking import BlockingScheduler
import os
import sys
from time import strftime, gmtime

def rolling():
    
    log_file_path="%s/access.log"%log_path
    if os.path.exists(log_file_path):
        file_size=os.path.getsize(log_file_path)
        mb_size=file_size*1.0/1024/1024
        if mb_size >= limit_file_size:
            print "rolling %s ,size=%s,limit=%s"%(log_file_path,mb_size,limit_file_size)
            os.system("mv %s %s/access.log.%s"%(log_file_path,log_path,strftime("%Y%m%d%H%M%S", gmtime())))
            os.system("kill -USR1 `cat %s/nginx.pid`"%log_path)
 
    
if __name__ == '__main__':
    
    cmd_agr=sys.argv
     
    if len(cmd_agr)>=4:
        
        global limit_file_size
        global log_path
        limit_file_size=int(cmd_agr[1]) #file size
        log_path=cmd_agr[2] #file path
        roll_time=cmd_agr[3] #time
        
        print limit_file_size,log_path,roll_time
        scheduler = BlockingScheduler()
        try:
            scheduler.add_job(rolling, 'interval', seconds=long(roll_time))
            scheduler.start()
        except(KeyboardInterrupt, SystemExit):
            scheduler.shutdown()
    
